package com.jic.data.exception;

import com.jic.common.base.vo.RestResult;
import com.jic.data.constant.consist.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zalando.problem.spring.web.advice.ProblemHandling;

/**
 * @author zhoufeng
 * @Description:
 * @date 2020-04-09 13:58
 */
@ControllerAdvice
@Slf4j
public class ExceptionTranslator implements ProblemHandling {

    @ExceptionHandler(AppRuntimeException.class)
    @ResponseBody
    public RestResult handleDeviceCenterException(AppRuntimeException ex) {
        return RestResult.error(ex.getStatus(), ex.getMessage());
    }


    @Override
    public ResponseEntity handleThrowable(Throwable ex, org.springframework.web.context.request.NativeWebRequest x){
        log.error("AOP Filter Exception",ex);
        RestResult restResult = new RestResult();
        restResult.setCode(ResponseCode.EXCEPTION);
        restResult.setMsg("系统异常请联系管理员");
        ResponseEntity<RestResult> entity = new ResponseEntity<RestResult>(restResult, HttpStatus.INTERNAL_SERVER_ERROR);
        return entity;
    }
}
